package cn.sunline.dreamvalue.config;

import org.apache.commons.lang.StringUtils;
import org.redisson.Redisson;
import org.redisson.config.Config;
import org.redisson.config.ReadMode;
import org.redisson.config.SentinelServersConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 11292
 * @title: RedissonConfig
 * @projectName dream-value
 * @description: TODO
 * @date 2020/5/1116:15
 */
@Configuration
@ConditionalOnBean(RedissonProperties.class)
public class RedissonConfig {

    @Autowired
    private RedissonProperties redissonProperties;
    /**
     　* @description: redison分布式锁配置
     　* @param []
     　* @return org.redisson.Redisson
     　* @author 11292
     　* @date 2020/5/11 14:01
     */
    @Bean
    public Redisson redisson() {
        Config config = new Config();
        if(null != redissonProperties.getClusterNodes() && redissonProperties.getClusterNodes().length > 0){
            config.useClusterServers().addNodeAddress(redissonProperties.getClusterNodes());
        }
        if(StringUtils.isEmpty(redissonProperties.getSingleNode())){
            config.useSingleServer().setAddress(redissonProperties.getSingleNode());
        }
        if(null != redissonProperties.getSentinelNodes() && redissonProperties.getSentinelNodes().length > 0){
            config.useSentinelServers()
                    .addSentinelAddress(redissonProperties.getSentinelNodes())
                    .setMasterName(redissonProperties.getSentinelMaster())
                    .setPassword(redissonProperties.getPassword());
        }
        return (Redisson) Redisson.create(config);
    }
}
